package com.we.es.support;

import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.properties.PropertiesIocLoader;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.google.common.io.Resources;
import com.uxuexi.core.common.util.ExceptionUtil;

/**
 * ElaticSearch服务器工具类，用于获取客户端实例，提供创建模板删除模板等与服务器设置相关的方法
 * 
 * @author   朱晓川
 */
public class ElaticSearchHelper {

	private static final Log log = Logs.get();

	//读取配置文件
	private static final PropertiesProxy ES_CONF;

	//集群名称
	public final static String CLUSTER_NAME;

	private static TransportClient client;

	//默认模板名称
	public final static String DEFAULT_TEMPLATE = "default_template";

	static {
		ES_CONF = new PropertiesIocLoader("elaticsearch/es-config.properties");
		CLUSTER_NAME = ES_CONF.get("cluster.name");
		List<String> nodes = ES_CONF.getList("cluster.nodes", ",");

		Settings settings = null;
		if (StringUtils.isNotBlank(CLUSTER_NAME)) {
			settings = Settings.builder().put("cluster.name", CLUSTER_NAME).put("client.transport.sniff", true).build();
		} else {
			throw ExceptionUtil.pEx("es集群名称不能为空,请检查");
		}

		client = new PreBuiltTransportClient(settings);
		try {
			for (String node : nodes) {
				String[] g = StringUtils.split(node, ":");
				client.addTransportAddress(new TransportAddress(InetAddress.getByName(g[0]), Integer.parseInt(g[1])));
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	private ElaticSearchHelper() {
	}

	public static TransportClient getClient() {
		return client;
	}

	/**
	 * 创建默认模板
	 */
	public static synchronized void createDefaultIndexTemplate() {
		try {
			URL url = Resources.getResource("elaticsearch/templates/default_template.js");
			String templateSource = Resources.toString(url, Charset.forName("utf-8"));
			PutIndexTemplateRequest req = new PutIndexTemplateRequest(DEFAULT_TEMPLATE);
			req.source(templateSource, XContentType.JSON);
			getClient().admin().indices().putTemplate(req).actionGet();
		} catch (Exception e) {
			log.error("can not create default index template.", e);
		}
	}

	/**
	 * 删除默认模板,慎用
	 */
	public static synchronized void deleteDefaultIndexTemplate() {
		try {
			DeleteIndexTemplateRequest req = new DeleteIndexTemplateRequest(DEFAULT_TEMPLATE);
			getClient().admin().indices().deleteTemplate(req).actionGet();
		} catch (Exception e) {
			log.error("can not delete default index template.", e);
		}
	}
}